package com.gyx.lottery.service.activitystatus.operator;

import com.gyx.lottery.dao.dataobject.ActivityDO;
import com.gyx.lottery.dao.mapper.ActivityMapper;
import com.gyx.lottery.dao.mapper.ActivityPrizeMapper;
import com.gyx.lottery.service.dto.ConvertActivityStatusDTO;
import com.gyx.lottery.service.enums.ActivityPrizeStatuseEnum;
import com.gyx.lottery.service.enums.ActivityStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends  AbstractActivityOperator{
    @Autowired
    ActivityPrizeMapper activityPrizeMapper;
    @Autowired
    ActivityMapper activityMapper;
    private static final Logger logger= LoggerFactory.getLogger(ActivityOperator.class);
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needCovert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //判断活动相关的参数是否合法
        Long activityId= convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus= convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId == null || targetActivityStatus == null){
          return false;
        }
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(activityDO == null || convertActivityStatusDTO.getTargetActivityStatus().name().
                equalsIgnoreCase(activityDO.getStatus())){
            return  false;
        }
        //校验处于INIT状态的奖品数是否大于0
       int count= activityPrizeMapper.countByPrizeStatus(activityId, ActivityPrizeStatuseEnum.INIT.name());
        if(count>0)
        {
            return  false;
        }
        return true;
    }

    @Override
    public Boolean covert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityPrizeStatuseEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if(activityId == null || targetPrizeStatus == null){
            return  false;
        }
        try {
            activityMapper.updateStatusById(activityId, targetPrizeStatus.name());
            return true;
        }
        catch (Exception e){
            return false;
        }
    }
}
